<?php
 
declare (strict_types = 1);

namespace app\xapply\controller;

use app\base\BaseController;
use app\xapply\model\App;
use think\facade\View;
use think\facade\Db;
use app\xapply\model\Rule;

class Myapp extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $list = App::with('category')->order('weigh', 'desc')->select()->toArray();
            $res['data'] = $list;
            return table_assign(0, '', $res);
        } else {
            return view();
        }
    }

    public function select(){
        $param = get_params();
        $type = $param['type'];
        $keyword = $param['keyword'];
        $new = [];
        if ($type == 'post'){
            $position = Db::name('Position')->where('status', '>=', 0)->order('create_time asc')->select();
            foreach ($position as $value){
                $new[] = [
                    'name' => $value['title'],
                    'value' => $value['id'],
                    'selected' => $keyword && $keyword == $value['id'] ? 'selected' : '',
                    'children' => [],
                ];
            }
        }
        if ($type == 'user'){
            $user = Db::name('admin')->order('id asc')->select();
            foreach ($user as $value){
                $new[] = [
                    'name' => $value['nickname'],
                    'value' => $value['id'],
                    'selected' => $keyword && $keyword == $value['id'] ? 'selected' : '',
                    'children' => [],
                ];
            }
        }
        return to_assign(0, '',$new);
    }

    public function perm(){
        $param = get_params();
        $object = App::find($param['id']);
        if (request()->isAjax()) {
            Rule::destroy(function($query) use ($param){
                $query->where('app_id', $param['id'])->where('type', $param['type']);
            });
            $bool = false;
            $a = $this->save($param, 'user_id');
            if ($a){
                $bool = true;
            }
            $b = $this->save($param, 'dept_id');
            if ($b){
                $bool = true;
            }
            $c = $this->save($param, 'group_id');
            if ($c){
                $bool = true;
            }
            $arr = ['is_use', 'is_notice', 'is_admin'];
            $field = $arr[$param['type']];
            $object->save([
                $field => $bool ? 1 : 0,
            ]);
            return to_assign();
        }
        $types = ['使用权限', '通知权限', '管理权限'];
        View::assign('detail', $object);
        View::assign('types', $types);
        View::assign('type', $param['type']);
        View::assign('id', $param['id']);
        View::assign('user_id', $this->value($param, 'user_id'));
        View::assign('dept_id', $this->value($param, 'dept_id'));
        View::assign('group_id', $this->value($param, 'group_id'));
        return view();
    }

    private function value($param, $field){
        $arr = Rule::where('app_id', $param['id'])->where('type', $param['type'])->where($field, '>', 0)->column($field);
        if ($arr){
            return implode(',', $arr);
        }
        return '';
    }

    private function save($param, $field){
        $id = trim($param[$field]);
        if ($id){
            $query = [];
            $arr = explode(',', $id);
            foreach ($arr as $value){
                $query[] = [
                    'app_id' => $param['id'],
                    $field => $value,
                    'type' => $param['type'],
                ];
            }
            if ($query){
                $object = new Rule();
                $object->saveAll($query);
                return true;
            }
        }
        return false;
    }

    //添加&编辑
    public function add()
    {
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        if ($id){
            $object = App::find($id);
            View::assign('detail', $object);
        }else{
            View::assign('detail', []);
        }
        View::assign('id', $id);
        if (request()->isAjax()) {
            if ($id){
                unset($param['id']);
                $param['updatetime'] = time();
            }else{
                $object = new App();
                $param['createtime'] = time();
            }
            $object->save($param);
            return to_assign();
        }
        return view();
    }
}
